package com.platform.usercenter.controller;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.platform.common.core.domain.entity.SysDept;
import com.platform.common.core.utils.StringUtils;
import com.platform.constants.SysUserConstants;
import com.platform.system.api.remote.api.RemoteSystemService;
import com.platform.system.api.remote.vo.SysDictData;
import com.platform.usercenter.domain.CalLeaderPlan;
import com.platform.usercenter.domain.dto.CalLeaderPlanDTO;
import com.platform.usercenter.domain.vo.LeaderInfoVO;
import com.platform.usercenter.service.ICalLeaderPlanService;
import com.platform.usercenter.service.ISysDeptService;
import com.platform.usercenter.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
//import com.platform.common.log.annotation.Log;
//import com.platform.common.log.enums.BusinessType;
import com.platform.common.security.annotation.RequiresPermissions;
import com.platform.common.core.web.controller.BaseController;
import com.platform.common.core.web.domain.AjaxResult;
import com.platform.common.core.utils.poi.ExcelUtil;
import com.platform.common.core.web.page.TableDataInfo;

/**
 * 领导排班计划主Controller
 *
 * @author platform
 * @date 2023-10-04
 */
@RestController
@RequestMapping("/leader/plan")
public class CalLeaderPlanController extends BaseController
{
    @Resource
    private ICalLeaderPlanService calLeaderPlanService;

    @Resource
    private ISysUserService userService;

    @Resource
    private ISysDeptService deptService;

    @Resource
    private RemoteSystemService remoteSystemService;

    /**
     * 查询领导排班计划主列表
     */
    @RequiresPermissions("gen:plan:list")
    @GetMapping("/list")
    public TableDataInfo list(CalLeaderPlanDTO calLeaderPlan)
    {
        startPage();
        List<CalLeaderPlan> list = calLeaderPlanService.selectCalLeaderPlanList(calLeaderPlan);
        return getDataTable(list);
    }

    /**
     * 查询用户列表
     * @param queryStr
     * @return
     */
    @GetMapping("/queryUserInfo")
    public TableDataInfo queryUserInfo(String queryStr){
        startPage();
        List<LeaderInfoVO> sysUserList = userService.queryUserListForPlan(queryStr);
        HashSet< LeaderInfoVO > leaderInfoVOS=new HashSet<>( sysUserList);
        List< LeaderInfoVO > leaderInfoVOS1=new ArrayList<>( );
        leaderInfoVOS1.addAll( leaderInfoVOS );
        TableDataInfo dataTable = getDataTable(leaderInfoVOS1);
        if (dataTable.getTotal() > 0){
            for (int i = 0; i < dataTable.getRows().size(); i++) {
                LeaderInfoVO o = (LeaderInfoVO) dataTable.getRows().get(i);
                o.setIndex(i+"");
            }
        }
        return dataTable;
    }


    /**
     * 导出领导排班计划主列表
     */
    @RequiresPermissions("gen:plan:export")
    //@Log(title = "领导排班计划主", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CalLeaderPlanDTO calLeaderPlan)
    {
        List<CalLeaderPlan> list = calLeaderPlanService.selectCalLeaderPlanList(calLeaderPlan);
        ExcelUtil<CalLeaderPlan> util = new ExcelUtil<CalLeaderPlan>(CalLeaderPlan.class);
        util.exportExcel(response, list, "领导排班计划");
    }

    /**
     * 获取领导排班计划主详细信息
     */
    @RequiresPermissions("gen:plan:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(calLeaderPlanService.selectCalLeaderPlanById(id));
    }

    /**
     * 批量新增领导排班计划主
     */
    //@Log(title = "领导排班计划主", businessType = BusinessType.INSERT)
    @PostMapping("/addBatch")
    public AjaxResult addBatch(@RequestBody CalLeaderPlanDTO calLeaderPlan) throws ParseException {
        return toAjax(calLeaderPlanService.addBatch(calLeaderPlan));
    }

    @RequiresPermissions("gen:plan:add")
    //@Log(title = "领导排班计划主", businessType = BusinessType.INSERT)
    @PostMapping("/addSingle")
    public AjaxResult addSingle(@RequestBody CalLeaderPlanDTO calLeaderPlan) throws ParseException {
        return toAjax(calLeaderPlanService.addSingle(calLeaderPlan));
    }


    /**
     * 修改领导排班计划主
     */
    @RequiresPermissions("gen:plan:edit")
    //@Log(title = "领导排班计划主", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody CalLeaderPlan calLeaderPlan)
    {
        return toAjax(calLeaderPlanService.updateCalLeaderPlan(calLeaderPlan));
    }

    /**
     * 删除领导排班计划主
     */
    @RequiresPermissions("gen:plan:remove")
    //@Log(title = "领导排班计划主", businessType = BusinessType.DELETE)
	@PostMapping("/delete")
    public AjaxResult remove(@RequestBody CalLeaderPlanDTO calLeaderPlanDTO)
    {
        List<Integer> ids = calLeaderPlanDTO.getIds();
        if (ids == null || ids.size() == 0){
            return AjaxResult.error("请选择排班计划后删除！");
        }
        return toAjax(calLeaderPlanService.deleteCalLeaderPlanByIds(ids));
    }

    /**
     * 获取部门树列表
     */
    @RequiresPermissions("system:user:list")
    @GetMapping("/deptTree")
    public AjaxResult deptTree(String dictType)
    {
        List< SysDictData > data=remoteSystemService.getDictInfo( dictType,SysUserConstants.FROM_SOURCE ).getData( );
        return success(data);
    }

    /**
     * 批量新增领导排班计划(指定多个日期)
     * @param calLeaderPlan
     * @return
     * @throws ParseException
     */
    @PostMapping("/addBatchByDateArr")
    public AjaxResult addBatchByDateArr(@RequestBody CalLeaderPlanDTO calLeaderPlan) throws ParseException {
        if(calLeaderPlan.getLeaderId()==null||StringUtils.isEmpty(calLeaderPlan.getLeaderName())
                ||StringUtils.isEmpty(calLeaderPlan.getLeaderPhone())||calLeaderPlan.getDateArr()==null
                ||calLeaderPlan.getDateArr().size()==0){
           return AjaxResult.error("必填参数缺失！");
        }
        return toAjax(calLeaderPlanService.addBatchByDateArr(calLeaderPlan));
    }
}
